<template>
  <div class="booking-box">
    <el-container style="max-width:1020px;margin:0 auto">
      <el-aside width="130px">
        <el-menu :default-active="activeIndex" @select="changeIndex">
          <el-menu-item index="1">单程</el-menu-item>
          <el-menu-item index="2">往返</el-menu-item>
        </el-menu>
      </el-aside>
      <el-container style="max-width:870px">
        <el-form label-width="70px" :model="formData" :rules="rules" ref="form">
          <el-row>
            <el-col :span="7">
              <city-picker
                prop="departureCity"
                type="出发城市"
                :cityList="cityListArr"
                placeholder="请选择出发城市"
                v-model:currCityName="formData.departureCity"
              ></city-picker>
            </el-col>
            <el-col :span="2">
              <el-button
                class="form-btn-switch"
                @click="exchangeCity"
                circle
              ></el-button>
            </el-col>
            <el-col :span="7">
              <city-picker
                prop="arrivalCity"
                type="到达城市"
                :cityList="cityListArr"
                placeholder="请选择到达城市"
                v-model:currCityName="formData.arrivalCity"
              ></city-picker>
            </el-col>
            <el-col :span="7" style="margin-left:25px">
              <passenger-picker
                prop="passengerInfo"
                v-model:passengerInfo="formData.passengerInfo"
              ></passenger-picker>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="7">
              <date-picker
                type="出发时间"
                prop="departureDate"
                v-model:dateTime="formData.departureDate"
                @changeTime="changeTime"
              ></date-picker>
            </el-col>
            <el-col :span="2">
              <el-button class="form-btn-deco" circle></el-button>
            </el-col>
            <el-col :span="7">
              <date-picker
                type="返程时间"
                :disabled="formData.radio"
                prop="returnDate"
                v-model:dateTime="formData.returnDate"
              ></date-picker>
            </el-col>
            <el-form-item>
              <el-button class="submit" @click="onSubmit">
                搜&nbsp;&nbsp;&nbsp;索
              </el-button>
            </el-form-item>
          </el-row>
        </el-form>
      </el-container>
    </el-container>
  </div>
</template>

<script lang="ts">
import {
  defineComponent,
  ref,
  reactive,
  provide,
  watch,
  onMounted,
  inject,
} from 'vue';
import CityPicker from '@/components/MainArea/components/CityPicker.vue';
import DatePicker from '@/components/MainArea/components/DatePicker.vue';
import PassengerPicker from '@/components/MainArea/components/PassengerPicker.vue';
import Decoration from '@/components/Common/Decoration.vue';
import router from '@/router';
import { useStore } from 'vuex';

export default defineComponent({
  name: 'BookingBox',
  components: {
    CityPicker,
    DatePicker,
    PassengerPicker,
    Decoration,
  },
  setup(props, context) {
    // const formData = ref({
    //   departureCity: '',
    //   arrivalCity: '',
    //   departureDate: null,
    //   returnDate: null,
    //   passengerInfo: '',
    //   pAmount: ref<number[]>([0, 0, 0]),
    //   // pAmount: ,
    // });
    const formData = ref(useStore().state.formData);
    const activeIndex = ref(formData.value.radio === true ? '1' : '2');
    const changeIndex = (index: string, indexPath: string) => {
      activeIndex.value = index;
    };
    provide('num', formData.value.pAmount);
    const name1 = '单程';
    const name2 = '往返';
    // const radio = ref(true);
    watch(
      () => activeIndex.value,
      () => {
        // radio.value = !radio.value;
        if (activeIndex.value === '1') {
          formData.value.radio = true;
        } else {
          formData.value.radio = false;
        }
      }
    );
    const cityListArr = reactive({
      hot: [{ id: 7, name: '北京市' }],
      A: [{ id: 7, name: '安阳市' }],
      B: [{ id: 7, name: '北京市' }],
      H: [{ id: 7, name: '杭州市' }],
    });
    const exchangeCity = () => {
      [formData.value.departureCity, formData.value.arrivalCity] = [
        formData.value.arrivalCity,
        formData.value.departureCity,
      ];
    };
    //自定义验证器
    const validateCity = (rule: any, value: string, callback: Function) => {
      if (value === '') {
        callback(new Error('到达城市不可为空'));
      } else {
        if (formData.value.arrivalCity === formData.value.departureCity) {
          callback(new Error('出发到达机场不能在同一个城市里!'));
        } else {
          callback();
        }
      }
    };
    const validatePass = (rule: any, value: string, callback: Function) => {
      if (value === '') {
        callback(new Error('乘机人数不可为空'));
      } else {
        if (
          formData.value.pAmount[0] * 3 <
          formData.value.pAmount[1] + formData.value.pAmount[2]
        ) {
          callback(new Error('未成年人数非法!'));
        } else {
          callback();
        }
      }
    };
    const validateDepartureDate = (
      rule: any,
      value: Date,
      callback: Function
    ) => {
      if (value === null) {
        callback(new Error('出发时间不可为空!'));
      } else {
        const today = new Date();
        //让当天时间加1
        if (
          today >
          new Date(
            (formData.value.departureDate as any).getTime() + 3600 * 1000 * 24
          )
        ) {
          callback(new Error('出发时间不可早于现在时间!'));
        } else {
          callback();
        }
      }
    };
    const validateReturnDate = (rule: any, value: Date, callback: Function) => {
      if (formData.value.radio === true) {
        callback();
      }
      if (value === null) {
        callback(new Error('返程时间不可为空!'));
      } else {
        if (
          (formData.value.departureDate as any) >
          (formData.value.returnDate as any)
        ) {
          callback(new Error('返程时间不可早于出发时间!'));
        } else {
          callback();
        }
      }
    };
    //表单数据验证
    const form = ref(null);
    const rules = {
      departureCity: [
        { required: true, message: '出发城市不可为空!', trigger: 'change' },
      ],
      arrivalCity: [{ validator: validateCity, trigger: 'change' }],
      passengerInfo: [
        // { required: true, message: '乘机人数不可为空', trigger: 'change' },
        { validator: validatePass, trigger: 'change' },
      ],
      // pAmount: [{ required: true, message: '乘机人错误', trigger: 'change' }],
      departureDate: [{ validator: validateDepartureDate, trigger: 'change' }],
      returnDate: [{ validator: validateReturnDate, trigger: 'change' }],
    };

    // const reload: any = inject('reload');
    const onSubmit = () => {
      (form.value as any).validate((valid: boolean) => {
        if (valid) {
          // alert('submit!');
          console.log('formData.value: ', formData.value);
          context.emit('reload');
          // reload();
          context.emit('getList');
        } else {
          console.log('信息不完整！');
          return false;
        }
      });
    };
    const changeTime = (date: Date) => {
      context.emit('changeDate', date);
    };
    return {
      activeIndex,
      changeIndex,
      formData,
      form,
      rules,
      name1,
      name2,
      cityListArr,
      exchangeCity,
      onSubmit,
      changeTime,
    };
  },
});
</script>

<style lang="scss" scoped>
@import '@/assets/css/config.scss';
.booking-box {
  // max-width: 1000px;
  text-align: left;
  margin: 0 auto;
  margin-bottom: 60px;
  width: 69%;
  height: 220px;
}
.el-menu-item,
.el-submenu__title {
  height: 110px;
  line-height: 110px;
  width: 100px;
  float: right;
  text-align: center;
  border: 1px solid $themeLight;
  border-right: none;
  font-weight: 800;
}
.el-menu-item:focus,
.el-menu-item:hover {
  background-color: rgb(220, 225, 233);
}
.el-menu-item.is-active {
  width: 120px;
  background-color: $themeColor;
  color: white;
  font-size: 17px;
}
.isActive {
  color: red;
}

form {
  box-sizing: border-box;
  padding-left: 10px;
  border-left: none;
  height: 220px;
  width: 870px;
  border: 1px solid $themeLight;
  border-left: none;
}
$height: 41px;
//行间距
:deep(.el-row) {
  margin-top: $height;
  margin-right: 0;
  padding-right: 0;
}
//中置按钮
.form-btn-switch {
  background: url('~@/assets/img/switch.png') center no-repeat;
}
.form-btn-deco {
  background: url('~@/assets/img/line-red.png') center no-repeat;
  cursor: default;
}
.form-btn-switch,
.form-btn-deco {
  margin-left: 8px;
  width: $height;
  padding: 0;
  float: left;
  border: none;
  background-size: $height $height;
}
//提交按钮
:deep(.submit) {
  background-color: $themeRed;
  color: white;
  width: 240px;
  margin-left: -32px;
  margin-top: 2px;
  font-size: 14px;
  font-weight: 700;
}

:deep(.el-input__inner) {
  width: 180px;
  font-size: 12px;
  padding-left: 10px;
  margin: 0;
}
:deep(.el-input__icon) {
  margin-top: -5px;
  padding-left: 145px;
}
:deep(.el-input__icon) {
  width: 25px;
  text-align: center;
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
  margin-right: -$height;
  line-height: 44px;
}
:deep(.el-form-item__label) {
  padding-right: 6px;
  font-size: 13px;
}
.el-col-7 {
  width: 260px;
  margin-left: 10px;
}
.el-col-2 {
  max-width: 4%;
}
</style>
